package com.sumfun.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sumfun.blog.common.ErrorCode;
import com.sumfun.blog.exception.BusinessException;
import com.sumfun.blog.mapper.ArticleTagReferencedMapper;
import com.sumfun.blog.model.domain.Article;
import com.sumfun.blog.model.domain.ArticleTagReferenced;
import com.sumfun.blog.model.request.ArticleUpdateRequest;
import com.sumfun.blog.service.ArticleTagReferencedService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 17337
* @description 针对表【article_tag_referenced(文章标签 )】的数据库操作Service实现
* @createDate 2025-01-09 15:35:02
*/
@Service
public class ArticleTagReferencedServiceImpl extends ServiceImpl<ArticleTagReferencedMapper, ArticleTagReferenced>
    implements ArticleTagReferencedService{

    @Override
    public Map<Long, List<ArticleTagReferenced>> getByArticleIds(List<Long> articleIds) {
        return articleIds.stream()
                .collect(Collectors.toMap(
                        articleId -> articleId, // keyMapper，使用文章ID作为键
                        articleId -> { // valueMapper，为每个文章ID查询相关的ArticleTagReferenced列表
                            QueryWrapper<ArticleTagReferenced> queryWrapper = new QueryWrapper<>();
                            queryWrapper.eq("article_id", articleId);
                            return this.list(queryWrapper);
                        },
                        (existing, replacement) -> existing
                ));
    }

    @Override
    public List<Long> getTagIdsByArticleId(Long articleId) {
        QueryWrapper<ArticleTagReferenced> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", articleId);
        List<ArticleTagReferenced> articleTagReferencedList = this.list(queryWrapper);
        return articleTagReferencedList.stream().map(articleTagReferenced -> {
            return articleTagReferenced.getTagId();
        }).collect(Collectors.toList());
    }
    @Override
    public Boolean deleteByArticleId(Long articleId) {
        QueryWrapper<ArticleTagReferenced> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("article_id", articleId);
        boolean remove = this.remove(queryWrapper);
        return remove;
    }

    /**
     * 更新文章  ：先删除所有关联标签，再新增标签
     * @param articleUpdateRequest 更新的数据
     */
    @Override
    public void updateArticle(ArticleUpdateRequest articleUpdateRequest) {
        if(articleUpdateRequest == null || articleUpdateRequest.getArticleId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long articleId = articleUpdateRequest.getArticleId();
        this.deleteByArticleId(articleId);
        // 获取新的标签ID列表并处理
        List<Long> newTagIdList = articleUpdateRequest.getTagIdList();
        if (newTagIdList != null && !newTagIdList.isEmpty()) {
            // 创建新的文章标签关联实体列表
            List<ArticleTagReferenced> newArticleTagList = newTagIdList.stream()
                    .map(tagId -> {
                        ArticleTagReferenced articleTagReferenced = new ArticleTagReferenced();
                        articleTagReferenced.setArticleId(articleId);
                        articleTagReferenced.setTagId(tagId);
                        return articleTagReferenced;
                    })
                    .collect(Collectors.toList());
            // 批量保存新的文章标签关联
            this.saveBatch(newArticleTagList);
        }
    }

}




